﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ECatalog.Models;
using ECatalog.Models.UserModel;

namespace ECatalog.Controllers
{
    public class UserController : Controller
    {
        private readonly AuthorizationService _authorizationService;
        private readonly IUserRepository userRepository;

        public UserController(IUserRepository userRepository, AuthorizationService authorizationService)
        {
            this.userRepository = userRepository;
            _authorizationService = authorizationService;
        }

        [Authorize]
        public ActionResult Index()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var users = userRepository.GetAllUsers();
            return View(users);
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Save(User user)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var isExistUser = userRepository.ExistUserByLogin(user.Login);
            if (isExistUser)
            {
                ModelState.AddModelError("Login", "UserController_Save_Login_Fail");
                return View("Register");
            }
            //var usr = userRepository.Save(user);
            //FormsAuthentication.SetAuthCookie(usr.Login, true); // выставляем куки для авторизованных лиц
            //return Redirect("/");

            userRepository.Save(user);
            return RedirectToAction("Index");
        }

        [Authorize]
        public ActionResult Register()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            return View();
        }

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(UserLoginDto model)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (_authorizationService.ValidateUser(model.Login, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.Login, true); // выставляем куки для авторизованных лиц
                if (!String.IsNullOrEmpty(model.ReturnUrl))
                {
                    return Redirect(model.ReturnUrl);
                }

                return RedirectToAction("Index", "Base");
            }

            ModelState.AddModelError("", "AccountController_Login_Fail");

            return View(model);
        }

        public ActionResult LogOff()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            FormsAuthentication.SignOut();
            return RedirectToAction("Index","Base");
        }

        public ActionResult Validate(string login)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var isExistUser = userRepository.ExistUserByLogin(login);
            return Json(isExistUser);
        }

        public ActionResult Delete(int id)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            userRepository.RemoveUserById(id);
            return RedirectToAction("Index", "User");
        }
    }
}